<template>
    <el-row class="login-container">
        <el-col :lg="16" :md="12" class="left">
            <div>
                <div>欢迎光临</div>
                <div>此站点是《vue3 + vite实战商城后台开发》视频课程的演示地址</div>
            </div>
        </el-col>
        <el-col :lg="8" :md="12" class="right">
            <h2 class="title">欢迎回来</h2>
            <div>
                <span class="line"></span>
                <span>账号密码登录</span>
                <span class="line"></span>
            </div>
            <el-form ref="formRef"  class="w-[250px]" :model="form" :rules="rules">
                <el-form-item prop="username">
                    <el-input  v-model="form.username"  placeholder="请输入用户名">
                        <template #prefix>
                            <el-icon><user /></el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password"  v-model="form.password" placeholder="请输入密码" show-password>
                        <template #prefix>
                            <el-icon><lock /></el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item>
                    <el-button round  color="#626aef" class="w-[250px]" type="primary" @click="register" :loading="loading">登 录</el-button>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>
</template>
<script setup>
    import { ref,reactive,onMounted,onBeforeUnmount } from 'vue'
    import { login ,getinfo} from '@/api/manager';
    // import { ElMessage } from 'element-plus'


    import { toast } from '@/composables/util'
    import { useRouter } from 'vue-router';
    import { setToken} from '@/composables/auth.js'
    import { useStore } from 'vuex';
    // 在哪里使用就调用         存const list = useLocalStorage('admin',123) 、、取 const list = useLocalStorage('admin') 
    // import { useLocalStorage } from '@vueuse/core'



    
    const store = useStore()
    const router = useRouter()
    const form = reactive({
        username:"admin",
        password:"admin"
    })
    const rules = {
        username:[
            { 
                required: true, 
                message: '用户名不能为空', 
                trigger: 'blur' 
            },
        ],
        password:[
            { 
                required: true, 
                message: '用户名不能为空', 
                trigger: 'blur' 
            },
        ]
    }   
    const formRef = ref(null)

    // 防止用户多次点击
    const loading = ref(false)

    // 登录 // 把登录和获取用户信息封装抽离放在vuex里面
    const register = () => {
    formRef.value.validate((valid)=>{
        if(!valid){
            return false
        }
        loading.value = true
        
        store.dispatch("login",form).then(res=>{
            toast("登录成功")
            router.push("/")
        }).finally(()=>{
            loading.value = false
        })
    })
    }


    // 监听回车事件
    function onKeyUp(e){
        if(e.key == "Enter") register()
    }

    // 添加键盘监听
    onMounted(()=>{
        document.addEventListener("keyup",onKeyUp)
    })
    // 移除键盘监听
    onBeforeUnmount(()=>{
        document.removeEventListener("keyup",onKeyUp)
    })




// 把登录和获取用户信息封装抽离放在vuex里面
    // const register =()=>{
    //  formRef.value.validate((item)=>{
    //         console.log(item)
    //         if(!item){  return false  }

    //          // 防止用户多次点击loading
    //         loading.value = true
    //         login(form.username,form.password).then(res=>{
    //             // 封装的消息通知
    //             toast('登录成功')

    //         //  存储token和用户信息

    //         //  封装好以后直接设置存token
    //         //  setToken(res.token)
    //          // 获取用户相关信息
    //         // getinfo().then(res2=>{  
    //         //     // 存在vuex里面
    //         //     store.commit('SET_USERINFO',res2)
    //         //     console.log(res2);
    //         // })

    //         //  跳转到登陆
    //          router.push('/')

    //         }).finally(()=>{
    //             loading.value = false
    //         })




    //     })
    // }

</script>

<style scoped lang="less" >
.login-container{
    // @apply min-h-screen bg-indigo-500;
    min-height: 100vh;
    font-weight: 500;
    background-color: #6366f1;
}
.login-container .left,.login-container .right{
    display: flex;
     align-items: center;
     justify-content: center
}
.login-container .right{
    background-color: #fff;
    flex-direction: column;
}
.left>div>div:first-child{
    // @apply font-bold text-5xl text-light-50 mb-4;
    font-weight: 700;
    font-size: 3rem; /* 48px */
    line-height: 1;
    color: #fff;
    margin-bottom: 1rem;
}
.left>div>div:last-child{
    // @apply text-gray-200 text-sm;
    color: #fff;
    font-size:  14px; /* 14px */
    line-height: 20px ; /* 20px */
}
.right .title{
    // @apply font-bold text-3xl text-gray-800;
    font-weight: 700;
    font-size: 1.875rem; /* 30px */
    line-height: 2.25rem; /* 36px */
    color: #000;
}
.right>div{
    // @apply flex items-center justify-center my-5 text-gray-300 space-x-2;
    display: flex;align-items: center;justify-content: center;
    margin-top: 1.25rem; /* 20px */
    margin-bottom: 1.25rem; /* 20px */
    color: #fff;
    margin-right: calc(0.5rem * var(--tw-space-x-reverse));
    margin-left: calc(0.5rem * calc(1 - var(--tw-space-x-reverse)));
}
.right .line{
    // @apply h-[1px] w-16 bg-gray-200;
    height: 1px;width: 4rem; /* 64px */background-color: #fff;
}


</style>